const Router = require('koa-router')
const multer = require('koa-multer');
const path = require('path')
const router = new Router({
  prefix: '/upload'
});

//配置上传
var storage = multer.diskStorage({
  //文件保存路径
  destination: function (req, file, cb) {
    cb(null, path.join(__dirname, '../assets/upload/')) //注意路径必须存在
  },
  //修改文件名称
  filename: function (req, file, cb) {
    var fileFormat = (file.originalname).split(".");
    cb(null, Date.now() + "." + fileFormat[fileFormat.length - 1]);
  }
})
const upload = multer({
  storage: storage
});

router.post('/file', upload.single('file'), async (ctx, next) => {
  ctx.body = {
    filename: ctx.req.filename
  }
})

module.exports = router;
